﻿namespace IP_Package.GUI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// quantization form
    /// </summary>
    public partial class QuantizationForm : Form
    {
        /// <summary>
        /// QUANTIZATION OBJECT
        /// </summary>
        private IPPackage.ImageOperation.Quantization quan = new IPPackage.ImageOperation.Quantization();

        /// <summary>
        /// buffer of orignal image
        /// </summary>
        private Color[,] bufferOfImage;

        /// <summary>
        /// flag to known if it save in orginal or not
        /// </summary>
        private bool saveOrNot = false;

        /// <summary>
        /// result image
        /// </summary>
        private Bitmap resultImg;

        /// <summary>
        /// Initializes a new instance of the <see cref="QuantizationForm"/> class.
        /// </summary>
        public QuantizationForm()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets a value indicating whether [save or not1].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [save or not1]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveOrNot1
        {
            get { return this.saveOrNot; }
            set { this.saveOrNot = value; }
        }

        /// <summary>
        /// Gets or sets the buffer of image.
        /// </summary>
        /// <value>
        /// The buffer of image.
        /// </value>
        public Color[,] BufferOfImage
        {
            get { return this.bufferOfImage; }
            set { this.bufferOfImage = value; }
        }

        /// <summary>
        /// Gets or sets the result img.
        /// </summary>
        /// <value>
        /// The result img.
        /// </value>
        public Bitmap ResultImg
        {
            get { return this.resultImg; }
            set { this.resultImg = value; }
        }

        /// <summary>
        /// Sets the info of orignal image.
        /// </summary>
        /// <param name="image">The orginal image.</param>
        /// <param name="width">The width of orignal image.</param>
        /// <param name="height">The height of orignal image.</param>
        /// <param name="bufferOfImage">The buffer of orignal image.</param>
        internal void SetInfo(Image image, string width, string height, Color[,] bufferOfImage)
        {
            this.picOrg.Image = image;
            this.LabWdth.Text = width;
            this.LabHth.Text = height;
            this.bufferOfImage = bufferOfImage;
        }

        /// <summary>
        /// Handles the Click event of the Button5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button5_Click(object sender, EventArgs e)
        {
            this.quan.BufferOfImage = this.BufferOfImage;
            try
            {
                int mask = Convert.ToInt32(this.txtbMask.Text, 2);
                this.resultImg = this.quan.QuantizationImg(mask);
                this.picbQuantization.Image = this.resultImg;
            }
            catch
            {
                if (this.picOrg.Image == null)
                {
                    MessageBox.Show("no image to make quanization operation");
                }
                else
                {
                    MessageBox.Show("operation fail. retry in another time");
                }
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox1 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("11111110", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("00000001", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox2.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("11111101", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("00000010", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox3 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox3.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("11111011", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("00000100", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox4 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox4.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("11110111", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("00001000", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox5 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox5.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("11101111", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("00010000", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox6 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox6.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("11011111", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("00100000", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox7 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox7_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox7.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("10111111", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("01000000", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the CheckBox8 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void CheckBox8_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox8.Checked == true)
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) & Convert.ToInt32("01111111", 2)), 2);
            }
            else
            {
                this.txtbMask.Text = Convert.ToString((Convert.ToInt32(this.txtbMask.Text, 2) | Convert.ToInt32("10000000", 2)), 2);
            }
        }

        /// <summary>
        /// Handles the Click event of the BTCAN control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BTCAN_Click(object sender, EventArgs e)
        {
            this.saveOrNot = false;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtOK_Click(object sender, EventArgs e)
        {
            if (this.resultImg == null)
            {
                this.quan.BufferOfImage = this.BufferOfImage;
                try
                {
                    int mask = Convert.ToInt32(this.txtbMask.Text, 2);
                    this.resultImg = this.quan.QuantizationImg(mask);
                    this.picbQuantization.Image = this.resultImg;
                }
                catch
                {
                    if (this.picOrg.Image == null)
                    {
                        MessageBox.Show("no image to make quanization operation");
                    }
                    else
                    {
                        MessageBox.Show("operation fail. retry in another time");
                    }
                }
            }

            if (this.picbQuantization.Image != null)
            {
                this.bufferOfImage = this.quan.NewBufferOfImage;
                this.SaveOrNot1 = true;
                this.Close();
            }
        }
    }
}
